package de.velvettear.squirrel.util;

import java.util.Vector;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.IntentFilter.MalformedMimeTypeException;
import android.nfc.NfcAdapter;
import android.nfc.tech.NfcF;
import android.util.Log;
import android.widget.Toast;
import de.velvettear.squirrel.kernel.NFCTag;
import de.velvettear.squirrel.kernel.Statics;

public class NFCHelper {

	private Activity activity;
	private boolean isForeground = false;
	private String[][] techLists;

	private NfcAdapter nfcAdapter;
	private NFCIntentListener listener;
	private IntentFilter[] intentFilters;
	private PendingIntent pendingIntent;

	public static interface NFCIntentListener {
		void nfcIntentDetected(Intent i, String action);
	}

	public NFCHelper(Activity activity) {
		this.activity = activity;

		nfcAdapter = NfcAdapter.getDefaultAdapter(activity);
		pendingIntent = PendingIntent.getActivity(activity, 0, new Intent(activity, activity.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
	}

	public void enableForeground() {
		if (!isForeground) {
			techLists = new String[][] {new String[] {NfcF.class.getName()}};
			
			IntentFilter ndefDetected = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
			IntentFilter techDetected = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
			IntentFilter tagDetected = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
			
			try {
				ndefDetected.addDataType("*/*");
			} catch (MalformedMimeTypeException e) {
				if (e.getMessage() != null) {
					Log.e("enableForeground()", e.getMessage());
				}
			}
			
			intentFilters = new IntentFilter[] {tagDetected, ndefDetected, techDetected};
			nfcAdapter.enableForegroundDispatch(activity, pendingIntent, intentFilters, techLists);

			isForeground = true;
		}
	}

	public void disableForeground() {
		if (isForeground) {
			nfcAdapter.disableForegroundDispatch(activity);
		}
	}

	public NFCIntentListener getListener() {
		return listener;
	}

	public void setNFCIntentListener(NFCIntentListener listener) {
		this.listener = listener;
	}

	public static int getNFCTech(String[] techList){
		for (String tech : techList) {
			if (tech.equals("android.nfc.tech.NfcV")) {
				return 1;
			}			
		}
		return 0;
	}

	public static String getUID_ISO_15693(byte[] b) throws Exception {
		String result = "";
		
		for(int i = b.length - 1; i >= 0 ;i--) {
			result += Integer.toString((b[i] & 0xff) + 0x100,16).substring(1);
		}
		
		return result.toUpperCase();
	}
		
	
	public static String getHexString(byte[] b) throws Exception {
		String result = "";
		
		for(int i=0; i< b.length; i++) {
			result += Integer.toString((b[i] & 0xff) + 0x100,16).substring(1);
		}
		
		return result.toUpperCase();
	}
	
	public static void executeTagAction(Context context, NFCTag tag) {
		if (tag.m_sType.equals(Statics.NFC_TYPE_APP)) {
			Intent appIntent = context.getPackageManager().getLaunchIntentForPackage(tag.m_sAction);
			if (appIntent != null) {
				context.startActivity(appIntent);
			} else {
				Toast.makeText(context, "Couldn't start app defined app properly!" , Toast.LENGTH_SHORT).show();
			}
			
			executeExtraActions(context, tag);
			return;
		}
		
		Toast.makeText(context, "No action for NFC-Tag (" + tag.m_sTagID + ") defined!", Toast.LENGTH_SHORT).show();
	}
	
	private static void executeExtraActions(Context context, NFCTag tag) {
		Vector<String> extras = tag.getExtras();
		for (String extra : extras) {
			if (extra.equals(Statics.NFC_EXTRA_WLAN)) {
				WLANHelper.enable(context);
			} else if (extra.equals(Statics.NFC_EXTRA_BLUETOOTH)) {
				BluetoothHelper.enable(context);
			}
		}
	}
	
}
